package com.woniuxy.woniuhis.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.*;
import com.woniuxy.woniuhis.exception.InpatientServiceException;
import com.woniuxy.woniuhis.service.DocAdviceService;
import com.woniuxy.woniuhis.service.InpatientService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 医嘱控制器
 */

@Controller
@RequestMapping("/docAdvice")
public class DocAdviceController {
	@Autowired
	private DocAdviceService docAdviceService;
	@Autowired
	private InpatientService inpatientService;

	@RequestMapping("/getListByInpatientId")
	@ResponseBody
	public ResponseInfo getListByInpatientId(String inpatientId, DocAdvice docAdvice, Integer pageNum) {
		System.out.println(docAdvice);
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			Inpatient inpatient = new Inpatient();
			inpatient.setInpatientId(inpatientId);
			docAdvice.setInpatient(inpatient);
			PageInfo<DocAdvice> pageInfo = docAdviceService.findDocAdviceListByCondition(docAdvice, pageNum, 5);
			responseInfo = new ResponseInfo(200, pageInfo, "ok");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg(e.getMessage());
			return responseInfo;
		}
	}

	@RequestMapping("/addAdvice")
	@ResponseBody
	public ResponseInfo addAdvice(String inpatientId, String note, HttpServletRequest req, String recipeMediId) {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			if (!StringUtils.isEmpty(inpatientId)) {
				DocAdvice docAdvice = new DocAdvice();
				docAdvice.setDocAdviceId(UUIDUtil.uuid32());
				// 从session当中获取检查和处方
				RecipeMedi recipeMedi = new RecipeMedi();
				recipeMedi.setRecipeMediId(recipeMediId);
				System.out.println("是否接受参数：" + recipeMediId);
				docAdvice.setRecipeMedi(recipeMedi);
				docAdvice.setDocAdviceDate(new Date());
				// todo 检查添加后session当中存一个
				Examination examination = (Examination) req.getSession().getAttribute("examinationSession");
				if (examination != null) {
					docAdvice.setExamination(examination);
				}
				docAdvice.setNote(note);
				docAdvice.setInpatient(inpatientService.findInpatientById(inpatientId));
				// emp需要登录后获取，此处先模拟一个emp
				Emp emp = new Emp();
				emp.setEmpId("234");
				docAdvice.setEmp(emp);
				docAdvice.setDocAdviceDate(new Date());
				System.out.println(docAdvice);
				docAdviceService.addDocAdvice(docAdvice);
				responseInfo = new ResponseInfo(200, docAdvice, "ok");
			} else {
				responseInfo.setState(500);
			}
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg(e.getMessage());
			return responseInfo;
		}
	}
}
